<script>
import { Message } from "element-ui";
export default {
  data() {
    return {
      loading: false,
      total: 50,
      course: {
        pageNo: 1,
        pageSize: 5,
        courseName: "",
        type: "",
      },
      typeList: "",
      tableData: [],
      courseForm: {
        courseName: "",
        tabularChart: "",
        courseTime: "",
        courseMount: "",
        courseIntroduce: "",
        applicablePopulation: "",
        typeId: "",
        learnHarvest: "",
        subscriptionNotice: "",
        teacherId: "",
        courseVidio: "",
      },
      title: "添加课程",
      dialogFormVisible: false,
      // 图片上传
      // uploadUrl: "http://localhost:8888/sfzy/file/upload",
      uploadUrl: "http://10.10.18.137:8888/sfzy/upload",

      dialogVisible: false,
      // baseURL: "http://localhost:8888/sfzy/file/load?fileName=",
      baseURL: "http://10.10.18.137:8888/sfzy/downLoad?filename=",

      dialogImageUrl: "",
      fileList: [],
      dialogVisible2: false,
      dialogImageUrl2: "",
      fileList2: [],
    }
  },
  methods: {
    // 清除数据
    clear() {
      this.dialogFormVisible = false;
      this.courseForm = {
        courseName: "",
        tabularChart: "",
        courseTime: "",
        courseMount: "",
        courseIntroduce: "",
        applicablePopulation: "",
        typeId: "",
        learnHarvest: "",
        subscriptionNotice: "",
        teacherId: "",
        courseVidio: "",
      };
      this.fileList = [];
    },

    //获取表格数据
    get() {
      this.loading = true;
      this.$http
        .get("/course/list", {
          params: this.course,
        })
        .then((res) => {
          console.log(res.data);
          if (res.data.status == 200) {
            this.tableData = res.data.data.list;
            this.total = res.data.data.total;
            // Message({
            //   type: "success",
            //   message: "获取数据成功",
            //   showClose: true,
            // });
            this.loading = false;
          } else {
            Message({
              type: "error",
              message: "获取数据失败",
            });
          }
        })
        .catch((e) => {
          console.log(e);
        });
    },
    getTypeList() {
      this.$http
        .get("/courseType/list")
        .then((res) => {
          console.log(res.data);
          if (res.data.status == 200) {
            this.typeList = res.data.data.list;
          }
        })
        .catch((e) => {
          console.log(e);
        });
    },

    // 新增的表单
    addForm() {
      this.getTypeList();
      this.title = "添加课程";
      this.dialogFormVisible = true;
    },
    // 添加
    add() {
      // let user = JSON.parse(sessionStorage.getItem("userid"));
      this.courseForm.teacherId = sessionStorage.getItem("userid");
      this.$http
        .post("/course/add", this.courseForm)
        .then((res) => {
          console.log(res.data);
          if (res.data.status == 200) {
            this.get();
            this.clear();
          } else {
          }
        })
        .catch((e) => {});
    },

    editForm(item) {
      this.getTypeList();
      this.title = "修改课程";
      this.dialogFormVisible = true;
      this.courseForm = item;
      // this.dialogVisible = true
      if (item.tabularChart != null) {
        this.fileList = [
          {
            name: item.tabularChart,
            url: this.baseURL + item.tabularChart,
          },
        ];
      }
      if (item.courseVidio != null) {
        this.fileList2 = [
          {
            name: item.courseVidio,
            url: this.baseURL + item.courseVidio,
          },
        ];
      }
    },
    edit() {
      this.$http
        .post("/course/update", this.courseForm)
        .then((res) => {
          console.log(res.data);
          if (res.data.status == 200) {
            this.get();
            this.clear();
          } else {
          }
        })
        .catch((e) => {});
    },

    // 删除
    remove(item) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http
            .post("/course/delete?id=" + item)
            .then((res) => {
              console.log(res.data);
              if (res.data.status == 200) {
                this.$message.success("");
                this.get();
              }
            })
            .catch((e) => {
              console.log(e);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    //分页
    handleSizeChange(val) {
      this.course.pageSize = val;
      this.get();
    },
    handleCurrentChange(val) {
      this.course.pageNo = val;
      this.get();
    },

    // 上传图片
    handleSuccess(res) {
      console.log("图片上传:" + res.message);
      this.courseForm.tabularChart = res.message;
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
      this.courseForm.tabularChart = "";
    },
    // 上传视频
    handleSuccess2(res) {
      console.log("视频上传:" + res.message);
      this.courseForm.courseVidio = res.message;
    },
    handlePictureCardPreview2(file) {
      this.dialogImageUrl2 = file.url;
      this.dialogVisible2 = true;
    },
    handleRemove2(file, fileList) {
      console.log(file, fileList);
      this.courseForm.courseVidio = "";
    },
  },
  watch: {
    radio() {
      this.goods.goodsStatus = this.radio;
      this.get();
    },
  },
  mounted() {
    this.get();
    this.getTypeList();
  },
};
</script>
<template>
  <div>
    <!-- 搜索栏 -->
    <div class="head">
      <el-row>
      <span>课程管理</span>
        <el-input
          clearable
          @clear="get"
          style="width: 200px; margin-right: 10px"
          v-model="course.courseName"
          placeholder="请输入课程名称"
        ></el-input>
        <!-- <el-input
          clearable
          @clear="get"
          style="width: 200px"
          v-model="course.type"
          placeholder="请输入文章类型"
        ></el-input> -->
        <el-select
          v-model="course.type"
          filterable
          clearable
          @clear="get"
          @change="get"
          placeholder="请选择课程"
          :click="typeList"
          ><el-option
            v-for="item in typeList"
            :key="item.id"
            :label="item.name"
            :value="item.name"
          ></el-option>
        </el-select>
        <el-button type="primary" @click="get">查找</el-button>
        <el-button type="primary" @click="addForm">新增</el-button>
        <!-- <el-button type="primary">管理</el-button> -->
      </el-row>

      <!-- 新增表单 -->
      <el-dialog
        :title="title"
        :visible.sync="dialogFormVisible"
        @close="clear"
      >
        <el-form :model="courseForm">
          <el-form-item>
            <span>课程标题</span>
            <el-input
              v-model="courseForm.courseName"
              style="width: 200px"
            ></el-input>
            <span>课程总时长</span>
            <el-input
              type="number"
              v-model="courseForm.courseTime"
              style="width: 200px"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <span>课程类型</span
            ><el-select
              v-model="courseForm.typeId"
              filterable
              placeholder="请选择"
              :click="getTypeList"
              ><el-option
                v-for="item in typeList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <span>课件总数量</span
            ><el-input
              type="number"
              v-model="courseForm.courseMount"
              style="width: 200px"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <span>课程简介</span
            ><el-input
              v-model="courseForm.courseIntroduce"
              style="width: 400px"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <span>适用人群</span
            ><el-input
              v-model="courseForm.applicablePopulation"
              style="width: 400px"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <span>学习收获</span
            ><el-input
              v-model="courseForm.learnHarvest"
              style="width: 400px"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <span>订阅须知</span
            ><el-input
              v-model="courseForm.subscriptionNotice"
              style="width: 400px"
            ></el-input>
          </el-form-item>
          <!-- <el-form-item>
            <span>驿站照片</span
            ><input type="file" @change="getFileName" style="width: 150px" />
          </el-form-item> -->

          <el-form-item>
            <span>课程封面</span>
            <!-- <el-input
            v-model="courseForm.courseVidio"
            style="width: 200px"
          ></el-input>       -->
            <el-upload
              :action="uploadUrl"
              list-type="picture-card"
              :file-list="fileList"
              :on-success="handleSuccess"
              :limit="1"
              :on-preview="handlePictureCardPreview"
              :on-remove="handleRemove"
              ><i class="el-icon-plus"></i>
            </el-upload>
            <el-dialog :visible.sync="dialogVisible">
              <img width="100%" :src="dialogImageUrl" />
            </el-dialog>
          </el-form-item>
          <el-form-item>
            <span>课程视频</span><el-upload
              :action="uploadUrl"
              :file-list="fileList2"
              :on-success="handleSuccess2"
              :limit="1"
              :on-preview="handlePictureCardPreview2"
              :on-remove="handleRemove2"
              ><el-button class="el-icon-plus"></el-button>
            </el-upload>
            <el-dialog :visible.sync="dialogVisible2">
              <video width="100%" :src="dialogImageUrl2" controls="controls" />
            </el-dialog>
          </el-form-item>
          <!-- <el-form-item>
            <quill-editor  placeholder="请输入文本"></quill-editor>
          </el-form-item> -->
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="clear">取 消</el-button>
          <el-button type="primary" @click="add" v-show="title == '添加课程'"
            >添加</el-button
          >
          <el-button type="primary" @click="edit" v-show="title == '修改课程'"
            >保 存</el-button
          >
        </div>
      </el-dialog>
    </div>

    <!-- 列表 -->
    <el-table
      :data="tableData"
      style="width: 100%"
      height="530"
      v-loading="loading"
      :header-cell-style="{ background: '#1890FF', color: 'white' }"
    >
      <el-table-column fixed prop="courseName" label="课程标题" width="200">
      </el-table-column>
      <el-table-column
        fixed
        prop="courseType.name"
        label="课程类型"
        width="120"
      >
      </el-table-column>
      <el-table-column fixed prop="courseTime" label="课程总时间" width="120">
      </el-table-column>
      <el-table-column fixed prop="courseMount" label="课件数量" width="120">
      </el-table-column>
      <el-table-column
        fixed
        prop="courseIntroduce"
        label="课程介绍"
        width="120"
      >
      </el-table-column>
      <el-table-column fixed prop="consult.name" label="咨询师姓名" width="120">
      </el-table-column>
      <el-table-column fixed prop="pjCount" label="评价数量" width="120">
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <!-- <el-button type="success" @click="selectById(scope.row)"
            >详情</el-button
          > -->
          <el-button type="success" @click="editForm(scope.row)"
            >编辑</el-button
          >
          <el-button type="danger" @click="remove(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="course.pageNo"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="course.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      style="margin-top: 10px"
    >
    </el-pagination>
  </div>
</template>
<style>
span {
  margin: 2px 10px;
}
.head {
  margin: 10px 5px;
}
.el-button {
  margin: 5px 20px;
}
</style>